<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\BusinessModel;
//use think\Console;
use think\Controller;
use think\Db;

class Business extends Base
{
    //商业用户
    public function index()
    {
//        halt(111);
        if (request()->isAjax()) {

            $param = input('param.');

            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $where = [];
            if (!empty($param['searchText'])) {

                $where['b.username'] = ['like', '%' . $param['searchText'] . '%'];
            }


            $business = new BusinessModel();

            $selectResult = $business->getBusinesssByWhere($where, $offset, $limit);

            // var_dump($selectResult);

            // 拼装参数
            foreach ($selectResult as $key => $vo) {

                $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['id']));
                $selectResult[$key]['photo'] = '<img src="' . $vo['photo'] . '" width="40px" height="40px">';
                $selectResult[$key]['datetime'] = date('Y-m-d H:i:s', $vo['datetime']);
                $selectResult[$key]['status'] = showOperate($this->status($vo['id'], $vo['status']));

                $selectResult[$key]['role'] == 1 ? $selectResult[$key]['role'] = '普通用户' : $selectResult[$key]['role'] = '商业用户';

            }


            $return['total'] = $business->getAllBusinesss($where);  // 总数据
            $return['rows'] = $selectResult;

            return json($return);

        }

        // halt(111);

        return $this->fetch();
    }

// 显示详情

    public function show($id)
    {

        $row=Db::table('wb_buser b')
            ->join('wb_buserinfo i','b.id=i.uid','left')
            ->field(['b.username','b.tel','b.status','b.id','i.qq','i.email','i.photo'])
            ->where('b.id='.$id)
            ->find($id);
//        halt($row);

        return view('business/show', [
            'title' => $row['username'] . '的详情',
            'row' => $row,

        ]);


    }

    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        return [
            '查看详情' => [
                'auth' => 'role/roleedit',
                'href' => url('business/show', ['id' => $id]),
                'btnStyle' => 'primary',
                'icon' => 'fa fa-paste'
            ],


        ];

    }

// 修改用户状态
//改变状态
    public function statusEdit()
    {
        $id = input('param.id');
        $s = input('param.status');
        $business = new BusinessModel();
        $flag = $business->editStatus($id, $s);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }

//状态按钮 1 为进行中 2为结束
    public function status($id, $s)
    {
        if ($s == 1) {
            return [
                '启用' => [
                    'auth' => 'role/roleedit',
                    'href' => "javascript:statusEdit(" . $id . "," . $s . ")",
                    'btnStyle' => 'info',
                    'icon' => 'fa fa-institution'
                ]
            ];
        } else {
            return [
                '禁用' => [
                    'auth' => 'role/roleedit',
                    'href' => "javascript:statusEdit(" . $id . "," . $s . ")",
                    'btnStyle' => 'info',
                    'icon' => 'fa fa-institution'
                ]
            ];
        }
    }
}

